﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infrastructure.Model.ApiResult;
using Infrastructure.Model.DI;
using Model.CommonModel.PageModel;
using Model.EntityModel.SysModel;
using Model.QueryModel.system;
using Model.ResultModel;
using Repository.IRepository.ISysRepository;
using Service.IService;
using SqlSugar;

namespace Service.Service
{
    [AppService(ServiceType = typeof(IAuthService), ServiceLifetime = LifeTime.Scoped)]
    public class AuthService : IAuthService
    {
        private readonly IUserRepository _userRepository;

        public AuthService(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public async Task<LoginResult> AuthenticateAsync(string username, string password)
        {
            try
            {
                var result = await _userRepository.LoginAsync(username, password);
                return result ?? throw new Exception("登录服务返回空结果");
            }
            catch (Exception ex)
            {
                throw new Exception("登录服务错误: " + ex.Message);
            }
        }
      
        public async Task<bool> ChangePasswordAsync(string userId, string oldPassword, string newPassword)
        {
            try
            {
                return await _userRepository.ChangePasswordAsync(userId, oldPassword, newPassword);
            }
            catch (Exception ex)
            {

                throw new Exception("修改密码失败: " + ex.Message);
            }

        }

        public async Task<bool> ResetPasswordAsync(string userId, string newPassword)
        {
            try
            {
                return await _userRepository.ResetPasswordAsync(userId, newPassword);
            }
            catch (Exception ex)
            {
                throw new Exception("重置密码失败: " + ex.Message);
            }
        }
    }
}
 
